package com.zhiyou100.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xml.internal.security.encryption.XMLCipher;
import com.zhiyou100.annotation.Auth;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Speaker;
import com.zhiyou100.model.Subject;
import com.zhiyou100.model.Video;
import com.zhiyou100.serice.CourseService;
import com.zhiyou100.serice.SpeakerService;
import com.zhiyou100.serice.VideoSerice;
import com.zhiyou100.util.FastDFSUtil;
import com.zhiyou100.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class VideoController {
    @Autowired
    private VideoSerice serice;
    @Autowired
    private SpeakerService service;
    @Autowired
    private CourseService courseService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value = "/video", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个查询全部Video的操作")
    public ResultObject findAll(Integer pageNo, Model model, Video video) {
        try {
            System.out.println("前台数据 : " + video);
            if (pageNo == null) {
                pageNo = 1;
            }
            System.out.println("这是当前页  : " + pageNo);
            List<Speaker> speakers = service.findAll();
            List<Course> courses = courseService.findAll();
            ArrayList<Object> lists = new ArrayList<Object>();
            PageHelper.startPage(pageNo, 10);
            List<Video> videos = serice.findAll(video.getTitle(), video.getSpearkerId(), video.getCourseId());
            System.out.println("这是 查询全部 : " + videos);
            PageInfo<Video> pageInfo = new PageInfo<Video>(videos);
            lists.add(videos);
            lists.add(speakers);
            lists.add(courses);
            lists.add(pageInfo);
            return new ResultObject("200", "查询全部video信息", lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultObject("404", "服务器繁忙", null);
        }
    }

    @RequestMapping(value = "/video/pageInfo/{pageNo}", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个查询全部Video进行分页的操作")
    public ResultObject findAllPage(@PathVariable("pageNo") Integer pageNo, Model model, Video video) {
        return findAll(pageNo, model, video);
    }

    @RequestMapping(value = "/video/{id}", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个通过id查询的操作")
    public ResultObject findById(@PathVariable("id") Integer id) {
        Jedis resource = jedisPool.getResource();
        ArrayList lists = new ArrayList();
        List<Course> courses;
        List<Speaker> speakers;
        try {
            if (resource.strlen("findByVideoId:" + id) != 0) {
                String s = resource.get("findByVideoId:" + id);
                List arrayLists = JSONArray.parseArray(s);
                for (int i = 0; i <= arrayLists.size(); i++) {
                    if (i == 2) {
                        courses = JSONArray.parseArray(arrayLists.get(i).toString(), Course.class);
                        lists.add(courses);
                    } else if (i == 0) {
                        Video video = JSONObject.parseObject(arrayLists.get(i).toString(), Video.class);
                        lists.add(video);

                    } else if (i == 1) {
                        speakers = JSONArray.parseArray(arrayLists.get(i).toString(), Speaker.class);
                        lists.add(speakers);
                    }
                }
                System.out.println("-------------------redis执行了---------------------");
            } else {
                Video vdieo = serice.findById(id);
                speakers = service.findAll();
                courses = courseService.findAll();
                lists.add(vdieo);
                lists.add(speakers);
                lists.add(courses);
                String o = JSONArray.toJSON(lists).toString();
                resource.set("findByVideoId:" + id, o);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            resource.close();
        }
        return new ResultObject("200", "查询全部video信息", lists);
    }

    @RequestMapping(value = "/video/{id}", method = RequestMethod.PUT)
    @ResponseBody@Auth("这是一个更新video的操作")
    public ResultObject update(@PathVariable("id") Integer id, @RequestBody Video video) {
        video.setId(id);
        System.out.println(video);
        serice.update(video);
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject();
    }

    @RequestMapping(value = "/video/{id}", method = RequestMethod.DELETE)
    @ResponseBody@Auth("这是一个删除video的操作")
    public ResultObject delete(@PathVariable("id") Integer... id) {
        System.out.println("前台数组 : " + Arrays.toString(id));
        for (int i : id) {
            serice.delete(i);
        }
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject("200", "删除成功", "success");
    }

    @RequestMapping(value = "/video/fileuploadImage", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个图片上传的video操作")
    public ResultObject upLoadImg(MultipartFile image) throws Exception {
        String upload = FastDFSUtil.upload(image);
        return new ResultObject("200", "删除成功", upload);
    }

    @RequestMapping(value = "/video/fileuploadVideo", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个视频上传的video操作")
    public ResultObject upLoadVideo(MultipartFile video) throws Exception {
        String upload = FastDFSUtil.upload(video);
        return new ResultObject("200", "删除成功", upload);
    }

    @RequestMapping(value = "/video/speakerAll", method = RequestMethod.GET)
    @ResponseBody@Auth("这是一个查询全部speaker操作")
    public ResultObject speakerAll() {
        List<Speaker> speakers = service.findAll();
        List<Course> courses = courseService.findAll();
        ArrayList<Object> lists = new ArrayList<Object>();
        lists.add(speakers);
        lists.add(courses);
        return new ResultObject("200", "删除成功", lists);
    }

    @RequestMapping(value = "/video/insert", method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个添加video操作")
    public ResultObject add(@RequestBody Video video) {
        System.out.println("这个是前台数据  :  " + video);
        serice.add(video);
        Jedis resource = jedisPool.getResource();
        resource.del("*");
        return new ResultObject();
    }

    @RequestMapping(value = "/video/courseId/{id}", method = RequestMethod.GET)
    @ResponseBody@Auth("这是一个通过courseId进行查询video的操作")
    public ResultObject findByCourseId(@PathVariable("id") Integer id) {
        System.out.println("这是other的查询");
        List<Video> byCourseId = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long strlen = jedis.strlen("findByCourseId:" + id);
            if (strlen != 0) {
                String s = jedis.get("findByCourseId:1");
                byCourseId = JSONArray.parseArray(s, Video.class);
                System.out.println("-------------------redis执行了---------------------");
            } else {
                byCourseId = serice.findByCourseId(id);
                String o = JSONArray.toJSON(byCourseId).toString();
                jedis.set("findByCourseId:1", o);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            jedis.close();
        }

        return new ResultObject("200", "查询成功", byCourseId);
    }

}
